<script setup>
import { ref, reactive, onBeforeMount } from "vue";
import useCatesStore from "../../store/catesStore";
import Dialog from "./dialog.vue";
import { listCategory } from "@/api/catesApi";
const catesStore = useCatesStore();
const tableData = ref([]);
onBeforeMount(() => {
  // 获取商品分类数据
});

const dialog = ref(null);
//获取一级分类

const getData = async () => {
  const res = await listCategory();
  // console.log(res);
  tableData.value = res.msg;
};
getData();
// 新增
function addCate() {
  // 打开对话框
  catesStore.openDialog();
}
// 编辑
function editCate(cate) {
  // console.log(cate,'编辑')
  dialog.value.editId = cate.id;
  dialog.value.cateForm.parent_id = cate.parent_id;
  dialog.value.cateForm.name = cate.name;
  dialog.value.cateForm.state = cate.state;

  // 打开对话框
  catesStore.openDialog();
}
// 删除
function confirmEvent(id) {
  // console.log(id,'删除')
  catesStore.delCateAction(id);
}
</script>

<template>
  <el-card>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商城管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类管理</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="right">
      <el-button @click="addCate" type="primary">新增商品分类</el-button>
    </div>
    <!-- 表格 -->
    <el-table
      :data="tableData"
      row-key="name"
      style="width: 100%"
      max-height="480"
      :tree-props="{ children: 'children' }"
    >
      <el-table-column
        prop="id"
        label="分类ID"
        width="80"
        fixed
      ></el-table-column>
      <el-table-column
        prop="parent_id"
        label="上级编号"
        width="80"
      ></el-table-column>
      <el-table-column
        prop="name"
        label="分类名称"
        width="150"
      ></el-table-column>

      <el-table-column prop="state" label="状态" width="150">
        <template v-slot="props">
          <el-tag
            v-if="props.row.state === 1"
            type="success"
            effect="dark"
            size="small"
            >启用</el-tag
          >
          <el-tag v-else type="danger" effect="dark" size="small">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" min-width="150">
        <template v-slot="prop">
          <el-button
            @click="editCate(prop.row)"
            type="primary"
            size="small"
            round
            >编辑</el-button
          >
          <!-- 气泡确认框 -->
          <el-popconfirm
            width="160px"
            title="你确定要删除吗？"
            @confirm="confirmEvent(prop.row.id)"
          >
            <template #reference>
              <el-button type="danger" size="small" round>删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格 -->
  </el-card>
  <!-- 对话框 -->
  <Dialog ref="dialog" />
</template>

<style lang="scss" scoped></style>
